package tech.lq0.dreamaticvoyage.gui.screen;

import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Inventory;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fluids.FluidStack;
import tech.lq0.dreamaticvoyage.Utils;
import tech.lq0.dreamaticvoyage.block.entity.FukamizuFermenterBlockEntity;
import tech.lq0.dreamaticvoyage.gui.menu.FukamizuFermenterMenu;
import tech.lq0.dreamaticvoyage.tools.RenderTool;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@OnlyIn(Dist.CLIENT)
public class FukamizuFermenterScreen extends AbstractContainerScreen<FukamizuFermenterMenu> {

    private static final ResourceLocation TEXTURE = Utils.loc("textures/gui/fukamizu_fermenter.png");

    public FukamizuFermenterScreen(FukamizuFermenterMenu pMenu, Inventory pPlayerInventory, Component pTitle) {
        super(pMenu, pPlayerInventory, pTitle);
        imageWidth = 176;
        imageHeight = 177;
    }

    @Override
    protected void renderBg(GuiGraphics pGuiGraphics, float pPartialTick, int pMouseX, int pMouseY) {
        int i = (this.width - this.imageWidth) / 2;
        int j = (this.height - this.imageHeight) / 2;
        pGuiGraphics.blit(TEXTURE, i, j, 0, 0, this.imageWidth, this.imageHeight);

        long energy = FukamizuFermenterScreen.this.menu.getEnergy();
        long tempAmount = FukamizuFermenterScreen.this.menu.getTempAmount();
        long tempCapacity = FukamizuFermenterScreen.this.menu.getTempCapacity();
        var blockEntity = FukamizuFermenterScreen.this.menu.blockEntity;

        // Energy
        float energyRate = Mth.clamp((float) energy / (float) FukamizuFermenterBlockEntity.MAX_ENERGY, 0, 1);
        pGuiGraphics.blit(TEXTURE, i + 22, j + 66 - (int) (31 * energyRate),
                177, 0, 4, (int) (31 * energyRate));

        // Input
        FluidStack inputFluid = blockEntity.clientInputFluid;
        RenderTool.renderFluid(pGuiGraphics, inputFluid, FukamizuFermenterBlockEntity.MAX_FLUID_CAPACITY, inputFluid.getAmount(),
                i + 63, j + 30, 0, 42, 16);

        // Output
        FluidStack outputFluid = blockEntity.clientOutputFluid;
        RenderTool.renderFluid(pGuiGraphics, outputFluid, FukamizuFermenterBlockEntity.MAX_FLUID_CAPACITY, outputFluid.getAmount(),
                i + 97, j + 30, 0, 42, 16);

        // Temp
        float tempRate = Mth.clamp((float) tempAmount / (float) tempCapacity, 0, 1);
        pGuiGraphics.blit(TEXTURE, i + 84, j + 65 - (int) (25 * tempRate),
                182, 25 - (int) (25 * tempRate), 8, 25);
    }

    @Override
    protected void renderTooltip(GuiGraphics pGuiGraphics, int pX, int pY) {
        super.renderTooltip(pGuiGraphics, pX, pY);

        int i = (this.width - this.imageWidth) / 2;
        int j = (this.height - this.imageHeight) / 2;

        var blockEntity = FukamizuFermenterScreen.this.menu.blockEntity;

        List<Component> tooltip = new ArrayList<>();
        tooltip.add(Component.translatable("des.dreamaticvoyage.umisu_current_energy",
                FukamizuFermenterScreen.this.menu.getEnergy(), FukamizuFermenterBlockEntity.MAX_ENERGY));

        if ((pX - i) >= 21 && (pX - i) <= 27 && (pY - j) >= 34 && (pY - j) <= 67) {
            pGuiGraphics.renderTooltip(this.font, tooltip, Optional.empty(), pX, pY);
        }

        if ((pX - i) >= 62 && (pX - i) <= 80 && (pY - j) >= 29 && (pY - j) <= 73) {
            if (!blockEntity.clientInputFluid.isEmpty()) {
                pGuiGraphics.renderTooltip(this.font,
                        List.of(
                                blockEntity.clientInputFluid.getDisplayName(),
                                Component.literal(blockEntity.clientInputFluid.getAmount() + " / " + FukamizuFermenterBlockEntity.MAX_FLUID_CAPACITY + " mB").withStyle(ChatFormatting.GRAY)
                        ), Optional.empty(), pX, pY);
            }
        }

        if ((pX - i) >= 96 && (pX - i) <= 114 && (pY - j) >= 29 && (pY - j) <= 73) {
            if (!blockEntity.clientOutputFluid.isEmpty()) {
                pGuiGraphics.renderTooltip(this.font,
                        List.of(
                                blockEntity.clientOutputFluid.getDisplayName(),
                                Component.literal(blockEntity.clientOutputFluid.getAmount() + " / " + FukamizuFermenterBlockEntity.MAX_FLUID_CAPACITY + " mB").withStyle(ChatFormatting.GRAY)
                        ), Optional.empty(), pX, pY);
            }
        }
    }

    @Override
    public void render(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
        this.renderBackground(pGuiGraphics);
        super.render(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
        this.renderTooltip(pGuiGraphics, pMouseX, pMouseY);
    }

    @Override
    protected void init() {
        super.init();
        this.titleLabelX = 8;
        this.titleLabelY = 2;
        this.inventoryLabelX = 8;
        this.inventoryLabelY = 85;
    }
}
